<?php

header("Content-type: text/html; charset=utf-8"); 
date_default_timezone_set('PRC');
testAction();
    
    function testAction(){
        $vcode = '00003';//$_GET['vcode'];
        $loginName = '360103199401090025';//$_GET['uname'];
        $loginPwd = '888888';// $_GET['pwd'];
        $zy = 6;
        
       
        $date = 'Sat%20Sep%2008%202018';urlencode(date("l M d Y",time()));
        $currDate = date('Y/m/d H:i:s');
        header('X-Accel-Buffering: no'); 
         $zylist = [
            'zyanbao'=>[
                '__VIEWSTATE'=>'/wEPDwULLTEzMDM3NTU1NjBkZBhf1dJHrB+C3/1o0/et8ec3sGMm3kDf4cXS228RXwAu',
                '__VIEWSTATEGENERATOR'=>'C2EE9ABB',
                '__EVENTVALIDATION'=>'/wEdAAyFNZdVRh4VoLHO/P2BIH14Y3plgk0YBAefRz3MyBlTcDPSlu16Yx4QbiDU+dddK1N1czP4tIzn+z/OwzXVca/top4oRunf14dz2Zt2+QKDEG0kWP0ndUqHt2zDLd14pCnVizgKih7MwNg1PehQbQToIBxXVHYUg4lfVuETpqR0n834O/GfAV4V4n0wgFZHr3chnb8gfpR2v6dlLZGnyPxNiKlbKjN9ItObAVMK57kbBT9YyyR4qjX0bKJ2X6bXV1PwZkLcrDQ6cjZ+mGY27xxzENfevlmI9qLdrvrQVaRGtw==',
                'loginUrl'=>'http://urdt.cn:9998/',
                'type'=>2,
                'baseUrl'=>'http://urdt.cn:9998/',
                'checkVedioUrl'=>'StudentClass/shipinSection.aspx/CheckCanWatch',
                'htmlUrl'=>'StudentClass/WatchVideo.aspx',
                'zy'=>''
            ],
            
            'zy6'=>[
           '__VIEWSTATE'=>'/wEPDwUKMTUyMzc0NjAxM2Rk5pSWiqLI2EzbrZdf8Tq40eItB0gL5/UL7U2PNAIBkPE=',
            '__VIEWSTATEGENERATOR'=>'C2EE9ABB',
            '__EVENTVALIDATION'=>'/wEdAAzf7jaajhkTo+yQgOSPJWiNY3plgk0YBAefRz3MyBlTcDPSlu16Yx4QbiDU+dddK1N1czP4tIzn+z/OwzXVca/top4oRunf14dz2Zt2+QKDEG0kWP0ndUqHt2zDLd14pCnVizgKih7MwNg1PehQbQToIBxXVHYUg4lfVuETpqR0n834O/GfAV4V4n0wgFZHr3chnb8gfpR2v6dlLZGnyPxNiKlbKjN9ItObAVMK57kbBT9YyyR4qjX0bKJ2X6bXV1MnZntd0U2axtwU8RH9Cvxmr85F6LRSleLiDh7igNdhDA==',
                'loginUrl'=>'http://139.199.65.78:8081/Login.aspx?ZY=6',
                'type'=>2,
                'baseUrl'=>'http://139.199.65.78:8081/',
                'checkVedioUrl'=>'StudentClass6/shipinSection6.aspx/CheckCanWatch',
                'htmlUrl'=>'StudentClass6/WatchVideo6.aspx',
                'zy'=>6,
        ]];
         global  $loginParams;
        $loginParams = $zylist['zy'.$zy];
        
        //cookie 保存目录
        $cookie_jar ="D:/{$loginName}.cookie";
        
        $loginUrl = $loginParams['loginUrl'];
        //模拟登录
        
        $loginData = [
            '__VIEWSTATE'=>$loginParams['__VIEWSTATE'],
            '__VIEWSTATEGENERATOR'=>$loginParams['__VIEWSTATEGENERATOR'],
            '__EVENTVALIDATION'=>$loginParams['__EVENTVALIDATION'],
            'txtUserName'=>$loginName,
            'txtPwd'=>$loginPwd,
            'ddlType'=>$loginParams['type'],
            'btnLogin'=>'',
            'txtLoginName'=>'',
            'txtDM'=>'',
            'txtFR'=>'',
            'zhjg'=>'',
            'zhfr'=>''
        ];
 
        setUserCookie($loginUrl, $cookie_jar, $loginData);
        $userInfo =   analysisCookie($cookie_jar);
        if($userInfo==false)
            exit('login fail');
       
        echo '<br/>登录成功<br/>';
        echo '用户信息:'.urldecode($userInfo['info']);
        
        $intVcode = intval($vcode);
        //检查视频是否可观看
        $dopostdata = ['Vcode'=>$vcode];
        echo '<br/>检查视频'.$vcode.'是否可以观看<br/>';
        $checkVedio =  doAjax($loginParams['baseUrl'].$loginParams['checkVedioUrl']."?dt={$date}", $dopostdata, $cookie_jar,true);
        echo '<br/>检查结果<br/>';
        if($checkVedio['d']){
            exit($checkVedio['d']);
        }else{
        	echo '检查通过..模拟打开视频页面<br/>';
            //可以看
            //模拟打开视频网页
        
            $html = getPage($loginParams['baseUrl'].$loginParams['htmlUrl']."?ids={$vcode}", $cookie_jar);
           
         
            //模拟提交我已看完
            $dopostdata = ['RVCode'=>$intVcode];
            sleep(2);
            $html2 = doAjax('http://139.199.65.78:8081/StudentClass6/WatchVideo6.aspx/UpdateStudyType?dt=Sat%20Sep%2008%202018',$dopostdata, $cookie_jar,true,$userInfo,$vcode,1);
           var_dump($html2);exit;
            if($html2['d']==''){
                exit('操作失败');
            }
            print_r($html2);exit;
            //模拟弹出答题
            if(!$zy){
            $html = getPage($loginParams['baseUrl']."EntpClass/Example.aspx?id={$intVcode}", $cookie_jar);
            //模拟答题
            $html2 =  doAjax($loginParams['baseUrl']."EntpClass/Xtyl1.ashx?cid=7&vid=1&uid=1000&c=Thu%20Jul%2019%202018%2013:33:28%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)", [],$cookie_jar,true);
            
            
            $html3 = doAjax($loginParams['baseUrl']."EntpClass/Xtyl1.ashx?classid={$intVcode}&uid={$userInfo['userid']}&c=Thu%20Jul%2019%202018%2013:33:33%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)", [],$cookie_jar,true,$userInfo,$vcode);
            }
            echo $vcode.'<br/>观看完毕 '.'<br/>';
        }
        
        
      /*   getPage("http://urdt.cn:9998/StudentClass/shipinSection.aspx", $cookie_jar);
        getPage("http://urdt.cn:9998/StudentClass/WatchVideo.aspx?ids=00002", $cookie_jar);
        $endVideo=['RVCode'=>'00002'];
        doPost("http://urdt.cn:9998/WatchVideo.aspx/UpdateStudyType?dt=".urlencode("Wed Jul 04 2018"),$endVideo ,$cookie_jar,true); */
        exit;
        
    }
    
     function getPage($url,$cookie_jar){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);//设定是否输出页面 内容
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar.'.login');
        $html=curl_exec($ch);
        curl_close($ch);
        return $html;
        
    }
    
    function setUserCookie($url,$cookie_jar,$loginParams){
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOBODY, false);//设定是否输出页面 内容
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
        
    }
    
     function  doPost($url,array $post ,$cookie_jar,$is_json=false){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
       
        if($is_json==true){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, ['contentType: application/json; charset=utf-8']);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post,JSON_UNESCAPED_UNICODE));
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,30);
        }else{
            curl_setopt($ch, CURLOPT_HEADER, false);
        }  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        $result=curl_exec($ch);
        curl_close($ch);
    }
    
    function  analysisCookie($cookieFile){
        $cookies = [];
        if(file_exists($cookieFile)){
            $content = file_get_contents($cookieFile);
            if(!$content)
                return false;
            
                $content = explode("\r\n", $content);
                $sessionId =explode("\t", $content[4]) ;
                $cookies['session'] = $sessionId[5].'='.$sessionId[6];
                for ($i=5;$i<count($content);$i++){
                    if(isset($content[$i])&&$content[$i]){
                        $temp = explode("\t", $content[$i]);
                        $cookies[$temp[5]] = $temp[6];
                    }
                }
                return $cookies;
        }
        return false;
    }
     function  doAjax($url,array $data ,$cookie_jar,$is_json=false,$cookieInfo=[],$vcode='',$isplain=false){
        $date =urlencode(date('Y-m-d H:i:s',time()-5000));
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_COOKIEFILE,$cookie_jar);
        curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36');
        
       
        if (!empty($data)&&$is_json==false){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }else{
            if($data){
            	$json  = json_encode($data);
            	$contentType = 'application/json;';
            	if($isplain==true){
            		$contentType = 'application/json; charset=UTF-8';
            	}
            	$header=['Content-Type:'.$contentType];
            	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            	curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            	curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
            }
        }
        global  $loginParams;
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
        if($cookieInfo){
          
           // $url = urlencode("{$loginParams['baseUrl']}StudentClass{$loginParams['zy']}/WatchVideo{$loginParams['zy']}.aspx?ids={$vcode}");
            $url= "http%3A//139.199.65.78%3A8081/StudentClass6/WatchVideo6.aspx%3Fids%3D{$vcode}";
            //$date = urlencode(date('Y').'/'.intval(date('m',time()+7200)).'/'.intval(date('d',time()+7200)).' '.date('H:i:s',time()+7200));
            $date= '2018/9/8%200%3A49%3A07';
			$sessionstr = "{$cookieInfo['session']}; checkUid={$cookieInfo['userid']}; id={$vcode}; info={$cookieInfo['info']}; nowtime=2318; RID={$vcode}; totaltime=2318; url={$url}; userid={$cookieInfo['userid']}; st={$date};PC1=%u6210%u529F";
			ECHO $sessionstr;
			ECHO '<BR/>';
			curl_setopt($curl, CURLOPT_COOKIE, $sessionstr);
        }else 
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $error = curl_error($curl);
        return json_decode($output,true);
        
        
    }


